package org.example.cas.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.example.cas.domain.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wh1107066
 */
@RestController
@RequestMapping(path = "/hello")
public class CasTestController {
    private static final Logger logger = LoggerFactory.getLogger(CasTestController.class);

//    @GetMapping(value = "/{ip}")
//    public String captcha(HttpServletRequest request, HttpServletResponse response, @PathVariable("ip") String ip) throws Exception {
//        HttpSession session = request.getSession();
//        session.setAttribute("captcha", "xxx");
//        logger.info("ip----->" + ip);
//        return "hello";
//
//    }


    /**
     * http://localhost:29410/hello/1?clientid=2
     */
    @GetMapping(value = "/1")
    public ResponseEntity<Map<String, Object>> helllo1(HttpServletRequest request) {
        String clientid = request.getParameter("clientid");
        logger.info("ip----->" + clientid);
        logger.info("iiiid----->" + clientid);
        Map<String, Object> rr = new HashMap<>();
        rr.put("clientid", clientid);
        rr.put("body", "hello-ip->" + clientid);
        ResponseEntity<Map<String, Object>> ok = ResponseEntity.ok(rr);
        logger.info("oooo ---> {}", ReflectionToStringBuilder.toString(ok, ToStringStyle.MULTI_LINE_STYLE));
        return ok;

    }

    /**
     * http://127.0.0.1:29410/hello/1?ip=2&id=3
     */
    @GetMapping(value = "/2")
    public String helllo2() {
        logger.info("================================");
        return "hello-ip";

    }

    /**
     * curl -i -X POST \
     * -H "Authorization:Basic YWRtaW46YWRtaW5AMTIzNA==" \
     * -H "Content-Type:application/json" \
     * -d \
     * '{
     * "id": 3
     * }' \
     * 'http://192.168.118.88:29410/hello/stu?page=1&size=10&id=444'
     */
    @PostMapping(value = "/stu")
    @ResponseBody
    public ResponseEntity<Student> student(HttpServletRequest request, @RequestBody Student student) {
        String id = request.getParameter("id");    // 获取的是 444   而不是  3，，， 只能在url的请求参数中获取到数据
        logger.info("id----->" + id);
        logger.info("student----->" + student);    // 获取的是 3  ， 获取不到 444
        // 对request的header 进行遍历
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            logger.info("headerName----->" + headerName);
            logger.info("headerName----->" + request.getHeader(headerName));
        }

        Student nnst = new Student();
        nnst.setId(id);
        nnst.setName(student.getName());

        ResponseEntity<Student> ok = ResponseEntity.ok(nnst);
        logger.info("设置值{}", ReflectionToStringBuilder.toString(ok, ToStringStyle.MULTI_LINE_STYLE));
        return ok;
    }
}
